package org.bb.initdata.service;

import cn.hutool.core.map.MapUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.http.HttpUtil;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.bb.initdata.mapper.ProductMapper;
import org.bb.initdata.model.Product;
import org.springframework.stereotype.Service;

@Service
public class ProductService extends ServiceImpl<ProductMapper, Product> {
    /**
     * 初始化数据
     *
     * @param urlTmp
     * @param pages
     */
    public void init(String urlTmp, int pages) {
        if (StrUtil.isBlank(urlTmp)) {
            throw new RuntimeException("urlTmp is blank!");
        }
        if (pages < 0) {
            throw new RuntimeException("pages error!");
        }
        for (int i = 0; i < pages; i++) {
            HttpUtil.createGet(StrUtil.format(urlTmp, i + 1))
                    .addHeaders(MapUtil.builder("Connection", "keep-alive")
                            .put("Cache-Control", "max-age=0")
                            .put("Upgrade-Insecure-Requests", "1")
                            .put("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/92.0.4515.131 Safari/537.36")
                            .put("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9")
                            .put("Accept-Language", "zh-CN,zh;q=0.9,en-US;q=0.8,en;q=0.7")
                            .build())
                    .cookie("PHPVALID=0028931e8fefe9dfcc898941d27e9f04")
                    .execute().body();

        }
    }
}
